package aTurk2;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

public class ProbabilityPricing implements Parameters{

	/**
	 * @param args
	 */
	private ArrayList<Problem> problemArr;
	private StringBuilder sb;
	public static void main(String[] args) throws Exception {
		ArrayList<Integer>costArrr=new ArrayList<Integer>();
		//read from file and fill up costArrr
		BufferedReader in=new BufferedReader(new FileReader("servers/cost.csv"));
		String line=in.readLine();
		while(line!=null){
			String costStr[]=line.split(",");
			costArrr.add(Integer.parseInt(costStr[1]));
			line=in.readLine();
		}
		in.close();
		
		ProbabilityPricing pp=new ProbabilityPricing();
		Random randGen=new Random();
		for(int i=0;i<PROBLEM_SIZE;++i){
			int j=i+1;
			String path="servers/7/"+j+".csv";
			pp.loadData(path);
			pp.addProbability(randGen,costArrr.get(i));
			int probabilityNewId=pp.shuffle(randGen);
			pp.save(j, probabilityNewId);
		}
	}

	private void addProbability(Random randGen,int cost){
		//int _cost=randGen.nextInt(MAX_QUERY_TIME)+1;	//1+MAX_QUERY_TIME
		int _cost=cost;
		Problem prob=getprobabilityStore(problemArr.size()+1, _cost);
		problemArr.add(prob);
	}
	private Problem getprobabilityStore(int id,int _cost){
		int maximumX=0; 
		double maximumRevenue=0;
		int cost=_cost;
		for(int x=0;x<TOTAL_L;x++){
			ArrayList<Problem>subSet=new ArrayList<Problem>();
			for(int i=0;i<problemArr.size();++i){
				problemArr.get(i).setProbabilityResVal(x);
				if(problemArr.get(i).resVal>COST_WEIGHT)
					subSet.add(problemArr.get(i));
			}
			double tempRevenue=getRevenue(subSet, cost, x);
			if(tempRevenue>maximumRevenue){
				maximumRevenue=tempRevenue;
				maximumX=x;
			}
		}
		
		Problem prob=new Problem(id, 0, maximumX+cost);
		prob.revenue=maximumX;
		return prob;
	}
	private double getRevenue(ArrayList<Problem>arrP,int cost,int x){
		double revenue=1;
		for(int i=0;i<arrP.size();++i){
			//it is actually probability bigger than... probabilitySmaller returns (1-p)
			revenue=revenue*(arrP.get(i).probabilitySmaller(x+cost));
		}
		revenue*=x;
		return revenue;
	}
	private int shuffle(Random rndGen){
		int location=rndGen.nextInt(problemArr.size());
		problemArr.get(location).id=problemArr.size();
		problemArr.get(problemArr.size()-1).id=location+1;
		Collections.sort(problemArr, new IdComperator());
		return (location+1);
	}
	public void save(int j, int optimalNewId) throws Exception{
		String strDirectoy ="servers/probability/"+j;
		// Create servers directory
		(new File(strDirectoy)).mkdirs();

		String strDirect2="servers/probability/srv";
		(new File(strDirect2)).mkdirs();
		String filename2=strDirect2+"/"+j+".srv";
		PrintWriter srvWrite=new PrintWriter(filename2);
		for(int i=0;i<problemArr.size();i++){
			int k=i+1;
			String filename=strDirectoy+"/"+k+".csv";
			PrintWriter pw=new PrintWriter(filename);
			String s1=problemArr.get(i).toExcelFormat();
			pw.write(s1);
			pw.close();
			
			srvWrite.append(problemArr.get(i).toSpecialFormat());
			srvWrite.append("\n");
		}
		srvWrite.close();
		
		//save the problem number, probability number, revenue
		if(j==1){
			sb=new StringBuilder();
			sb.append("problem number,probability number,revenue");
			sb.append("\n");
		}
		
		String str=j+","+optimalNewId+","+problemArr.get(optimalNewId-1).revenue;
		sb.append(str);
		sb.append("\n");
		if(j==PROBLEM_SIZE){
			String path="servers/probability/probabilityInfo.csv";
			PrintWriter optimalWrite=new PrintWriter(path);
			optimalWrite.append(sb.toString());
			optimalWrite.close();
		}
	}
	private void loadData(String path) throws Exception{
		problemArr=new ArrayList<Problem>();
		BufferedReader in=new BufferedReader(new FileReader(path));
		String line=in.readLine();
		while(line!=null){
			problemArr.add(new Problem(line));
			line=in.readLine();
		}
		in.close();
	}
}
